"""
用户账户相关功能：注册、短信、登录、注销
"""
from django.http import JsonResponse
from django.shortcuts import render, HttpResponse
from web.forms.account import RegisterModelForm, SendSmsForm


def register(request):
    if request.method == 'GET':
        form = RegisterModelForm()
        return render(request, 'register.html', {"form": form})

    form = RegisterModelForm(data=request.POST)
    if form.is_valid():
        # 验证通过，保存到数据库（密码要是密文）
        form.save()
        return JsonResponse({"status": True, "data": "/login/"})

    return JsonResponse({"status": False, "error": form.errors})

def send_sms(request):
    """ 发送短信 """

    # mobile_phone = request.POST.get('mobile_phone')
    # tpl = request.GET.get('tpl')

    # 校验
    form = SendSmsForm(request, data=request.GET)
    if form.is_valid():
        return JsonResponse({"status": True})

    return JsonResponse({"status": False, "error": form.errors})